package com.wisdom.benefit.jufa.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;

import androidx.appcompat.widget.AppCompatImageView;
import androidx.core.content.ContextCompat;

import com.wisdom.benefit.jufa.R;
import com.wisdom.benefit.jufa.utils.DisplayUtil;

public class BorderImageView extends AppCompatImageView {

    private Paint mPaint;
    private int mRadius;
    private RectF mRectF = new RectF();
    private boolean mShowBorder = true;

    public BorderImageView(Context context) {
        this(context, null);
    }

    public BorderImageView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public BorderImageView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaint.setColor(ContextCompat.getColor(getContext(), R.color.divider));
        mPaint.setStrokeWidth(1);
        mPaint.setStyle(Paint.Style.STROKE);
        mRadius = DisplayUtil.dip2px(getContext(), 4);
    }

    public BorderImageView setBorderColor(int borderColor) {
        mPaint.setColor(borderColor);
        postInvalidate();
        return this;
    }

    public BorderImageView setBorderWidth(int borderWidth) {
        mPaint.setStrokeWidth(borderWidth);
        postInvalidate();
        return this;
    }

    public BorderImageView setRadius(int radius) {
        mRadius = radius;
        postInvalidate();
        return this;
    }

    public void setShowBorder(boolean showBorder) {
        mShowBorder = showBorder;
        postInvalidate();
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldW, int oldH) {
        super.onSizeChanged(w, h, oldW, oldH);
        mRectF.set(0, 0, w, h);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (mShowBorder)
            canvas.drawRoundRect(mRectF, mRadius, mRadius, mPaint);
    }
}
